package com.sailfish.springbootdemo.service.db7.Impl;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db4.User2Dao;
import com.sailfish.springbootdemo.dao.db7.TbFlowDetailDao;
import com.sailfish.springbootdemo.dao.db7.TbFlowMainDao;
import com.sailfish.springbootdemo.dao.db7.TbFlowProcessDetailDao;
import com.sailfish.springbootdemo.dao.db7.TbFlowProcessMainDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db7.TbFlowDetail;
import com.sailfish.springbootdemo.service.db7.TbFlowDetailService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class TbFlowDetailServiceImpl implements TbFlowDetailService {
    @Autowired
    private User2Dao user2Dao;

    @Autowired
    private TbFlowDetailDao tbFlowDetailDao;

    @Autowired
    private TbFlowProcessMainDao tbFlowProcessMainDao;

    @Autowired
    private TbFlowProcessDetailDao tbFlowProcessDetailDao;

    @Autowired
    private TbFlowMainDao tbFlowMainDao;

    @Override
    public Result getApproverUserByDepartId(String data) {
        try {
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                String _dept_id = jsonObject.getString("dept_id");

                Integer dept_id = null;
                if (_dept_id != null && !_dept_id.isEmpty()) {
                    try
                    {
                        dept_id=Integer.parseInt(_dept_id);
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                return ResultUtil.success(user2Dao.getUserInfoByDept(dept_id));
            }
            else
            {
                return ResultUtil.error(500, "参数为空", "", null);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }


    @Override
    public Result getFlowListByType(String data) {
        try {
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                String _query_type = jsonObject.getString("query_type");
                String _flow_type = jsonObject.getString("flow_type");

                if (_query_type == null || _query_type.isEmpty() ) {
                    return ResultUtil.error(500, "query_type 不能为空", "", null);
                }

                List<String> listString=new ArrayList<>();
                if(_query_type.equals("model"))
                {
                    if (_flow_type == null || _flow_type.isEmpty() ) {
                        listString.add("designType");
                        listString.add("deviceType");
                    }
                    else
                    {
                        listString.add(_flow_type);
                    }
                }

                if(_query_type.equals("task"))
                {
                    if (_flow_type == null || _flow_type.isEmpty() ) {
                        listString.add("vpl");
                        listString.add("device");
                        listString.add("design");
                    }
                    else
                    {
                        listString.add(_flow_type);
                    }
                }

                List<TbFlowDetail> tbFlowDetails = tbFlowDetailDao.getFolwListByType2(listString);
                if (tbFlowDetails != null && tbFlowDetails.size() > 0) {
                    for (TbFlowDetail detail : tbFlowDetails) {
                        String flowApprovers = detail.getFlow_approver();
                        if (flowApprovers != null && !flowApprovers.isEmpty()) {
                            // 使用逗号分隔 flowApprovers 字符串，并转换为 List
                            List<String> flowApproverList = Arrays.asList(flowApprovers.split(","));
                            detail.setFlow_approver_list(flowApproverList);

                            detail.setFlow_approver_info(user2Dao.getUserInfoByIds(flowApproverList));
                        }
                    }
                }

                return ResultUtil.success(tbFlowDetails);

            }
            else
            {
                return ResultUtil.error(500, "参数为空", "", null);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }

    @Override
    public Result insertFlow(String data) {
        try {
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                TbFlowDetail param = new TbFlowDetail();

                JSONObject jsonObject = JSONObject.parseObject(data);
                String _flow_type = jsonObject.getString("flow_type");
                String _flow_name = jsonObject.getString("flow_name");
                String _flow_level = jsonObject.getString("flow_level");
                String _flow_approver = jsonObject.getString("flow_approver");
                String _sort = jsonObject.getString("sort");
                String _create_user = UserHeaderHolder.getUserId();//jsonObject.getString("create_user");
                String _remark = jsonObject.getString("remark");

                if (_flow_type != null && !_flow_type.isEmpty()) {
                    param.setFlow_type(_flow_type);
                }
                else
                {
                    return ResultUtil.error(500, "flow_type 不能为空", "", null);
                }

                if (_flow_name != null && !_flow_name.isEmpty()) {
                    param.setFlow_name(_flow_name);
                }
                else
                {
                    return ResultUtil.error(500, "flow_name 不能为空", "", null);
                }

                if (_flow_level != null && !_flow_level.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_flow_level);
                        param.setFlow_level(temp);
                    } catch (NumberFormatException e) {
                        return ResultUtil.error(500, "flow_level 格式错误", "", null);
                    }
                }
                else
                {
                    return ResultUtil.error(500, "flow_level 不能为空", "", null);
                }

                if (_flow_approver != null && !_flow_approver.isEmpty()) {
                    param.setFlow_approver(_flow_approver);
                }
                else
                {
                    return ResultUtil.error(500, "flow_approver 不能为空", "", null);
                }

                if (_sort != null && !_sort.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_sort);
                        param.setSort(temp);
                    }
                    catch (NumberFormatException e) {
                        return ResultUtil.error(500, "sort 格式错误", "", null);
                    }
                }

                if (_create_user != null && !_create_user.isEmpty()) {
                    param.setCreate_user(_create_user);
                }

                if (_remark != null && !_remark.isEmpty()) {
                    param.setRemark(_remark);
                }

                param.setCreate_time(new Date());


                Integer result= tbFlowDetailDao.insertFlow(param);

                if (result > 0) {
                    return ResultUtil.success("新增成功");
                }
                else {
                    return ResultUtil.error(500, "新增失败", "", null);
                }

            }
            else
            {
                return ResultUtil.error(500, "参数为空", "", null);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }

    @Override
    @Transactional(transactionManager = "MysqlTransactionManager7")
    public Result updateFlow(String data) {
        try {
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                TbFlowDetail param = new TbFlowDetail();

                JSONObject jsonObject = JSONObject.parseObject(data);
                String _id = jsonObject.getString("id");
                String _flow_type = jsonObject.getString("flow_type");
                String _flow_name = jsonObject.getString("flow_name");
                String _flow_level = jsonObject.getString("flow_level");
                String _flow_approver = jsonObject.getString("flow_approver");
                String _sort = jsonObject.getString("sort");
                String _update_user = UserHeaderHolder.getUserId();//jsonObject.getString("update_user");
                String _remark = jsonObject.getString("remark");

                if (_id != null && !_id.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_id);
                        param.setId(temp);
                    }
                    catch (NumberFormatException e) {
                        return ResultUtil.error(500, "id 格式错误", "", null);
                    }
                }
                else
                {
                    return ResultUtil.error(500, "id 不能为空", "", null);
                }
                /**
                if (_flow_type != null && !_flow_type.isEmpty()) {
                    param.setFlow_type(_flow_type);
                }
                else
                {
                    return ResultUtil.error(500, "flow_type 不能为空", "", null);
                }

                if (_flow_name != null && !_flow_name.isEmpty()) {
                    param.setFlow_name(_flow_name);
                }
                else
                {
                    return ResultUtil.error(500, "flow_name 不能为空", "", null);
                }

                if (_flow_level != null && !_flow_level.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_flow_level);
                        param.setFlow_level(temp);
                    } catch (NumberFormatException e) {
                        return ResultUtil.error(500, "flow_level 格式错误", "", null);
                    }
                }
                else
                {
                    return ResultUtil.error(500, "flow_level 不能为空", "", null);
                }*/

                if (_flow_approver != null && !_flow_approver.isEmpty()) {
                    param.setFlow_approver(_flow_approver);
                }
                else
                {
                    return ResultUtil.error(500, "flow_approver 不能为空", "", null);
                }
/**
                if (_sort != null && !_sort.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_sort);
                        param.setSort(temp);
                    }
                    catch (NumberFormatException e) {
                        return ResultUtil.error(500, "sort 格式错误", "", null);
                    }
                }
*/
                if (_update_user != null && !_update_user.isEmpty()) {
                    param.setUpdate_user(_update_user);
                }

                if (_remark != null && !_remark.isEmpty()) {
                    param.setRemark(_remark);
                }

                param.setUpdate_time(new Date());



                //先要获取一下原始的流程节点信息
                TbFlowDetail oldFlowDetail = tbFlowDetailDao.getFlowDetailById(Integer.parseInt(_id));
                if (oldFlowDetail == null) {
                    return ResultUtil.error(500, "找不到该流程节点", "", null);
                }

                Integer result= tbFlowDetailDao.updateFlow(param);

                if (result > 0) {
                    //还要去更新审核主表,当前审核人和原来一样的都更新掉
                    tbFlowProcessMainDao.updateFlowProcessMainFromDeatil(oldFlowDetail.getFlow_approver(),param.getFlow_approver(),oldFlowDetail.getFlow_type(),3);

                    //再去更新未审核的审核流程
                    tbFlowProcessDetailDao.updateFlowProcessDetailFromDeatil(oldFlowDetail.getFlow_approver(),param.getFlow_approver(),oldFlowDetail.getFlow_type(),2);

                    return ResultUtil.success("更新成功");
                }
                else {
                    return ResultUtil.error(500, "更新失败", "", null);
                }

            }
            else
            {
                return ResultUtil.error(500, "参数为空", "", null);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }

    @Override
    public Result deleteFlow(String data) {
        try {
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                TbFlowDetail param = new TbFlowDetail();

                JSONObject jsonObject = JSONObject.parseObject(data);
                String _id = jsonObject.getString("id");

                if (_id != null && !_id.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_id);
                        param.setId(temp);
                    }
                    catch (NumberFormatException e) {
                        return ResultUtil.error(500, "id 格式错误", "", null);
                    }
                }
                else
                {
                    return ResultUtil.error(500, "id 不能为空", "", null);
                }
                Integer result= tbFlowDetailDao.deleteFlowById(param.id);

                if (result > 0) {
                    return ResultUtil.success("删除成功");
                }
                else {
                    return ResultUtil.error(500, "删除失败", "", null);
                }

            }
            else
            {
                return ResultUtil.error(500, "参数为空", "", null);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }

    @Override
    public  Result getFlowList(String data)
    {
        try {
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                String _query_type = jsonObject.getString("query_type");
                String _flow_type = jsonObject.getString("flow_type");

                if (_query_type == null || _query_type.isEmpty() ) {
                    return ResultUtil.error(500, "query_type 不能为空", "", null);
                }

                List<String> listString=new ArrayList<>();
                if(_query_type.equals("model"))
                {
                    if (_flow_type == null || _flow_type.isEmpty() ) {
                        listString.add("designType");
                        listString.add("deviceType");
                    }
                    else
                    {
                        listString.add(_flow_type);
                    }
                }

                if(_query_type.equals("task"))
                {
                    if (_flow_type == null || _flow_type.isEmpty() ) {
                        listString.add("vpl");
                        listString.add("device");
                        listString.add("design");
                    }
                    else
                    {
                        listString.add(_flow_type);
                    }
                }

                return ResultUtil.success(tbFlowMainDao.getFlowMainList(listString));

            }
            else
            {
                return ResultUtil.error(500, "参数为空", "", null);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }

}
